home *** CD-ROM | disk | FTP | other *** search
/ NetObjects Fusion 7 / Fusion7.iso / NetObjects Fusion / data1.cab / Language_Resource_-_English / Components / AdBanner / DynamicBanner.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-10-18  |  6.5 KB  |  238 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Event;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9.  
  10. public class DynamicBanner extends Applet implements Runnable {
  11.    Thread kicker = null;
  12.    public Image image = null;
  13.    BannerTransition transition = null;
  14.    public int currentBanner;
  15.    public int nextBanner;
  16.    int numOfBanners;
  17.    int numOfTransitionTypes;
  18.    int currentTransitionType;
  19.    long delay;
  20.    boolean mouseInsideApplet;
  21.    boolean mouseDown;
  22.    int componentWidth;
  23.    int componentHeight;
  24.    int autoResize;
  25.    public BannerData[] banners;
  26.    String[] transitionClasses;
  27.  
  28.    public void finishInit() {
  29.       if (this.delay == -1L) {
  30.          String s = ((Applet)this).getParameter("delay");
  31.  
  32.          try {
  33.             this.delay = Long.parseLong(s);
  34.          } catch (NumberFormatException var5) {
  35.             this.delay = 3L;
  36.          }
  37.  
  38.          this.transitionClasses = new String[this.numOfBanners];
  39.  
  40.          for(int t = 0; t < this.numOfBanners; ++t) {
  41.             s = ((Applet)this).getParameter("banner" + Integer.toString(t));
  42.             int fieldStart = s.lastIndexOf(",");
  43.             this.transitionClasses[t] = s.substring(fieldStart + 1) + "Transition";
  44.          }
  45.  
  46.          this.banners[this.nextBanner].initPixels(this.componentWidth, this.componentHeight);
  47.          this.mouseInsideApplet = false;
  48.       }
  49.    }
  50.  
  51.    public void init() {
  52.       String s = ((Applet)this).getParameter("bgcolor");
  53.       if (s != null) {
  54.          Color color = new Color(Integer.parseInt(s.substring(1), 16));
  55.          ((Component)this).getParent().setBackground(color);
  56.          ((Component)this).getParent().repaint();
  57.          ((Component)this).setBackground(color);
  58.       }
  59.  
  60.       s = ((Applet)this).getParameter("banner0");
  61.       this.numOfBanners = 1;
  62.  
  63.       for(String var4 = ((Applet)this).getParameter("banner1"); var4 != null; var4 = ((Applet)this).getParameter("banner" + Integer.toString(this.numOfBanners))) {
  64.          ++this.numOfBanners;
  65.       }
  66.  
  67.       s = ((Applet)this).getParameter("autoResize");
  68.       this.autoResize = Integer.parseInt(s);
  69.       this.banners = new BannerData[this.numOfBanners];
  70.       int holder = (int)(Math.random() * (double)this.numOfBanners);
  71.       if (holder > this.numOfBanners - 1) {
  72.          holder = this.numOfBanners - 1;
  73.       }
  74.  
  75.       this.currentBanner = this.nextBanner = holder;
  76.       this.componentWidth = ((Component)this).size().width;
  77.       this.componentHeight = ((Component)this).size().height;
  78.  
  79.       while(!this.parseBannerData()) {
  80.          if (++this.nextBanner >= this.numOfBanners) {
  81.             this.nextBanner = 0;
  82.          }
  83.       }
  84.  
  85.       this.delay = -1L;
  86.    }
  87.  
  88.    public boolean mouseUp(Event evt, int x, int y) {
  89.       if (this.banners[this.currentBanner].link == null) {
  90.          return false;
  91.       } else {
  92.          this.kicker.stop();
  93.          ((Applet)this).getAppletContext().showDocument(this.banners[this.currentBanner].link);
  94.          return true;
  95.       }
  96.    }
  97.  
  98.    public void paint(Graphics g) {
  99.       g.drawImage(this.image, 0, 0, Color.white, this);
  100.    }
  101.  
  102.    boolean parseBannerData() {
  103.       String s = ((Applet)this).getParameter("banner" + Integer.toString(this.nextBanner));
  104.       int fieldEnd = s.indexOf(",");
  105.       int secondFieldEnd = s.lastIndexOf(",");
  106.       String imageFile = s.substring(0, fieldEnd);
  107.       if (imageFile.length() <= 0) {
  108.          return false;
  109.       } else {
  110.          Image newImage = ((Applet)this).getImage(((Applet)this).getDocumentBase(), imageFile);
  111.          s = s.substring(fieldEnd + 1, secondFieldEnd);
  112.  
  113.          URL link;
  114.          try {
  115.             link = new URL(((Applet)this).getDocumentBase(), s);
  116.          } catch (MalformedURLException var9) {
  117.             link = null;
  118.          }
  119.  
  120.          ((Component)this).prepareImage(newImage, this);
  121.          if (this.autoResize == 1) {
  122.             if (newImage.getWidth(this) != this.componentWidth || newImage.getHeight(this) != this.componentHeight) {
  123.                newImage = newImage.getScaledInstance(this.componentWidth, this.componentHeight, 2);
  124.                ((Component)this).prepareImage(newImage, this);
  125.             }
  126.          } else {
  127.             ((Component)this).prepareImage(newImage, this);
  128.          }
  129.  
  130.          this.banners[this.nextBanner] = new BannerData(link, newImage);
  131.          if (this.image == null) {
  132.             this.image = newImage;
  133.          } else {
  134.             this.banners[this.nextBanner].initPixels(this.componentWidth, this.componentHeight);
  135.          }
  136.  
  137.          return true;
  138.       }
  139.    }
  140.  
  141.    public void run() {
  142.       while((((Component)this).checkImage(this.image, this) & 16) == 0 && (((Component)this).checkImage(this.image, this) & 32) == 0) {
  143.          try {
  144.             Thread.sleep(600L);
  145.          } catch (InterruptedException var13) {
  146.          }
  147.       }
  148.  
  149.       this.finishInit();
  150.  
  151.       while(Thread.currentThread() == this.kicker) {
  152.          long nextBannerTime = System.currentTimeMillis() + this.delay * 1000L;
  153.          this.currentBanner = this.nextBanner;
  154.          if (++this.nextBanner >= this.numOfBanners) {
  155.             this.nextBanner = 0;
  156.          }
  157.  
  158.          if (this.banners[this.nextBanner] == null) {
  159.             while(!this.parseBannerData()) {
  160.                if (++this.nextBanner >= this.numOfBanners) {
  161.                   this.nextBanner = 0;
  162.                }
  163.             }
  164.  
  165.             try {
  166.                Thread.sleep(120L);
  167.             } catch (InterruptedException var8) {
  168.             }
  169.          }
  170.  
  171.          this.currentTransitionType = this.currentBanner;
  172.  
  173.          try {
  174.             this.transition = (BannerTransition)Class.forName(this.transitionClasses[this.currentTransitionType]).newInstance();
  175.             this.transition.initialize(this, this.componentWidth, this.componentHeight);
  176.          } catch (Exception e) {
  177.             ((Throwable)e).printStackTrace();
  178.          }
  179.  
  180.          if (System.currentTimeMillis() < nextBannerTime) {
  181.             try {
  182.                Thread.sleep(nextBannerTime - System.currentTimeMillis());
  183.             } catch (InterruptedException var11) {
  184.             }
  185.          }
  186.  
  187.          Graphics g = ((Component)this).getGraphics();
  188.  
  189.          for(int f = 0; f < this.transition.numOfFrames; ++f) {
  190.             this.image = this.transition.frames[f];
  191.             g.drawImage(this.image, 0, 0, this);
  192.             ((Component)this).getToolkit().sync();
  193.  
  194.             try {
  195.                Thread.sleep((long)this.transition.delay);
  196.             } catch (InterruptedException var10) {
  197.             }
  198.          }
  199.  
  200.          this.image = this.banners[this.nextBanner].image;
  201.          g.drawImage(this.image, 0, 0, this);
  202.          ((Component)this).getToolkit().sync();
  203.          g.dispose();
  204.          this.transition.clearFrames();
  205.          this.transition = null;
  206.  
  207.          try {
  208.             Thread.sleep(120L);
  209.          } catch (InterruptedException var9) {
  210.          }
  211.       }
  212.  
  213.    }
  214.  
  215.    public void start() {
  216.       if (this.kicker == null) {
  217.          this.nextBanner = this.currentBanner;
  218.          this.image = this.banners[this.currentBanner].image;
  219.          this.transition = null;
  220.          this.kicker = new Thread(this);
  221.          this.kicker.start();
  222.       }
  223.  
  224.    }
  225.  
  226.    public void stop() {
  227.       if (this.kicker != null) {
  228.          this.kicker.stop();
  229.          this.kicker = null;
  230.       }
  231.  
  232.    }
  233.  
  234.    public void update(Graphics g) {
  235.       this.paint(g);
  236.    }
  237. }
  238.